<?php 
class ModelLocalisationOrderStatus extends Model {
	public function addOrderStatus($data) {
		foreach ($data['order_status'] as $language_id => $value) {
			$value['color'] = $data['color'];
			$value['language_id'] = $language_id;
			if (isset($order_status_id)) {
				$value['order_status_id'] = $order_status_id;
				$this->db->insert('order_status', $value);
			} else {
				$order_status_id = $this->db->insert('order_status', $value);
			}
		}
	}

	public function editOrderStatus($order_status_id, $data) {
		$this->db->query("DELETE FROM  @@order_status WHERE order_status_id = " . (int)$order_status_id);
		
		foreach ($data['order_status'] as $language_id => $value) {
			$value['color'] = $data['color'];
			$value['order_status_id'] = $order_status_id;
			$value['language_id'] = $language_id;
			$this->db->insert('order_status', $value);
		}
	}
	
	public function deleteOrderStatus($order_status_id) {
		$this->db->query("DELETE FROM  @@order_status WHERE order_status_id = " . (int)$order_status_id);
	}
		
	public function getOrderStatus($order_status_id) {
		return $this->db->queryOne("SELECT * FROM  @@order_status WHERE order_status_id = " . (int)$order_status_id . " AND language_id = " . (int)C('config_language_id'));
	}
		
	public function getOrderStatuses($filter = null) {
      	
		$sql = "SELECT * FROM  @@order_status WHERE language_id = '" . (int)C('config_language_id') . "'";
		
		if ($filter) {
			$sql .= $filter->sort('name');
			$sql .= $filter->limit();
		}	
		
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	
	public function getOrderStatusDescriptions($order_status_id) {
		$order_status_data = array();
		
		$query = $this->db->query("SELECT * FROM  @@order_status WHERE order_status_id = " . (int)$order_status_id);
		
		foreach ($query->rows as $result) {
			$order_status_data[$result['language_id']] = array('name' => $result['name'], 'color' => $result['color']);
		}
		
		return $order_status_data;
	}
	
	public function getTotalOrderStatuses() {
      	return $this->db->queryOne("SELECT COUNT(*) AS total FROM  @@order_status WHERE language_id = " . (int)C('config_language_id'));
	}	
}
?>